package cn.chencq.proxy.timer.service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.chencq.proxy.constants.Constants;

public class ProxyIPService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ProxyIPService.class);

	private static ExecutorService threadPool = 
						new ThreadPoolExecutor(
								200, 
								200,
								5 * 1000,
								TimeUnit.MINUTES, 
								new LinkedBlockingDeque<Runnable>(),
								new ThreadCreateFactory());

	static class ThreadCreateFactory implements java.util.concurrent.ThreadFactory {

		final ThreadGroup group;
		final AtomicInteger threadNumber = new AtomicInteger(1);
		final String namePrefix;

		public ThreadCreateFactory() {
			SecurityManager s = System.getSecurityManager();
			group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
			namePrefix = "pool-thread-fetch";
		}

		@Override
		public Thread newThread(Runnable r) {
			Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
			if (t.isDaemon())
				t.setDaemon(false);
			if (t.getPriority() != Thread.NORM_PRIORITY)
				t.setPriority(Thread.NORM_PRIORITY);
			return t;
		}

	}

	public static boolean process = false;
	public static int pagesize = 20;

	public boolean get() {
		process = true;

		Set<Future<Map<String, Integer>>> sfSet = new HashSet<Future<Map<String, Integer>>>();
		
		String ip66Url = "http://m.66ip.cn/nmtq.php?isp=0&anonymoustype=0&start=&ports=&export=&ipaddress=&area=1&proxytype=2&api=66ip&getnum=1000";
		sfSet.add(threadPool.submit(new ProxyIPCallable(ip66Url, Constants.IP66.getValue())));

		String proxy360Url = "http://www.proxy360.cn/Region/China";
		sfSet.add(threadPool.submit(new ProxyIPCallable(proxy360Url, Constants.PROXY360.getValue())));

		String ip181Url = "http://www.ip181.com/";
		sfSet.add(threadPool.submit(new ProxyIPCallable(ip181Url, Constants.IP181.getValue())));

		String gbjUrl = "http://www.goubanjia.com/free/gngn/index.shtml";
		sfSet.add(threadPool.submit(new ProxyIPCallable(gbjUrl, Constants.GBJ.getValue())));

		for (int page = 1; page < pagesize; page++) {

			String kdlUrl = "http://www.kuaidaili.com/free/inha/{page}/";
			String xiciUrl = "http://www.xicidaili.com/nn/{page}";
			String ip84Url = "http://ip84.com/gn/{page}";
			String nianShaoUrl = "http://www.nianshao.me/?page={page}";
			String kxdlUrl = "http://www.kxdaili.com/dailiip/1/{page}.html#ip";
			String yundlUrl = "http://www.ip3366.net/free/?stype=1&page={page}";

			kdlUrl = kdlUrl.replace("{page}", page + "");
			sfSet.add(threadPool.submit(new ProxyIPCallable(kdlUrl, Constants.KDL.getValue())));

			xiciUrl = xiciUrl.replace("{page}", page + "");
			sfSet.add(threadPool.submit(new ProxyIPCallable(xiciUrl, Constants.XICI.getValue())));

			ip84Url = ip84Url.replace("{page}", page + "");
			sfSet.add(threadPool.submit(new ProxyIPCallable(ip84Url, Constants.IP84.getValue())));

			nianShaoUrl = nianShaoUrl.replace("{page}", page + "");
			sfSet.add(threadPool.submit(new ProxyIPCallable(nianShaoUrl, Constants.NIANSHAO.getValue())));

			kxdlUrl = kxdlUrl.replace("{page}", page + "");
			sfSet.add(threadPool.submit(new ProxyIPCallable(kxdlUrl, Constants.KXDL.getValue())));

			yundlUrl = yundlUrl.replace("{page}", page + "");
			sfSet.add(threadPool.submit(new ProxyIPCallable(yundlUrl, Constants.YDL.getValue())));
			
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		Map<String, Integer> countMap = new HashMap<String, Integer>();

		for (Future<Map<String, Integer>> future : sfSet) {
			try {
				Map<String, Integer> res = future.get();
				for (Map.Entry<String, Integer> en : res.entrySet()) {
					String key = en.getKey();
					int value = en.getValue();
					if (countMap.containsKey(key)) {
						value += countMap.get(key);
					}
					countMap.put(key, value);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int count = 0;
		for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
			String key = entry.getKey();
			int value = entry.getValue();
			LOGGER.info(key + "代理获取到代理ip数量:" + value);
			count += value;
		}

		LOGGER.info("current fetch data save success,count:{}", count);
		process = false;

		return true;
	}
}
